//
//  MRAllCity_ViewController.m
//  Travel_World
//
//  Created by dllo on 15/10/15.
//  Copyright © 2015年 TLS. All rights reserved.
//

#import "MRAllCity_ViewController.h"
#import "MRAllCity_CollectionViewCell.h"
#import "QTAFNnetWorkTool.h"
#import "UIImageView+WebCache.h"
#import "MRAllCity_Model.h"
#import "MRCityViewController.h"

@interface MRAllCity_ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) NSMutableArray *array;

@end

@implementation MRAllCity_ViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.translucent = NO;
    self.title = @"全部城市";
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    self.tabBarController.tabBar.hidden = YES;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createCollectionView];
    
    [self createData];
    
}

#pragma mark 数据解析
- (void)createData
{
    
    [QTAFNnetWorkTool getUrl:self.url body:nil hudSuperView:self.view result:QTJSON headerFile:nil success:^(id result) {
        
        self.array = [MRAllCity_Model baseModelByArr:result[@"data"]];
        
        [self.collectionView reloadData];
        
    }];
    
}

#pragma mark 创建collectionView
- (void)createCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(160 * WID, 180 * HEI);
    flowLayout.sectionInset = UIEdgeInsetsMake(10 * HEI, 20 * WID, 10 * HEI, 20 * WID);
    
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) collectionViewLayout:flowLayout];
    
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.collectionView];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    // 注册collectionView的cell
    [self.collectionView registerClass:[MRAllCity_CollectionViewCell class] forCellWithReuseIdentifier:@"city"];
    
}

#pragma mark collectionView的cell的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.array.count;
}

#pragma mark 创建collectionView的cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MRAllCity_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"city" forIndexPath:indexPath];
    
    if (self.array.count > indexPath.item) {
        
        MRAllCity_Model *model = self.array[indexPath.item];
        
        [cell.ImageView sd_setImageWithURL:[NSURL URLWithString:model.photo]placeholderImage:[UIImage imageNamed:@"placeholder1"]];
        
        cell.cnname.text = model.catename;
        cell.enname.text = model.catename_en;
        cell.beenstr.text = model.beenstr;
        cell.title.text = [NSString stringWithFormat:@"代表景点:%@",model.representative];
        
        NSLog(@" model  %@",model.representative);
        
        NSLog(@"%@",cell.title.text);
    }
    
    
    return cell;
}

#pragma mark collectionView的点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MRCityViewController *cityVC = [[MRCityViewController alloc] init];
    
    NSString *url = [NSString stringWithFormat:@"http://open.qyer.com/qyer/footprint/city_detail?city_id=%@&client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&lat=38.88261036070509&lon=121.5397574228773&oauth_token=55d7f8a1931dedc7d5a8f24ab63426d0&page=1&track_app_channel=App%%2520Store&track_app_version=6.7&track_device_info=iPhone5%%2C4&track_deviceid=41AE760B-510C-4778-AF4D-177B387E0F3B&track_os=ios%%25209.0.2&track_user_id=6852801&v=1",[self.array[indexPath.row] Id]];
    
    cityVC.url = url;
    
    [self.navigationController pushViewController:cityVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
